package com.elong.businesstravel.protocol.baserequest;

import java.net.URLEncoder;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.network.BaseHttpRequest;
import com.android.support.jhf.network.loopj.android.http.RequestParams;
import com.android.support.jhf.utils.FileUtils;
import com.elong.businesstravel.base.mcrypt.MCrypt;
import com.elong.businesstravel.base.utils.AndroidManifestUtil;
import com.elong.businesstravel.base.utils.AppUserId;
import com.elong.businesstravel.include.Version;
import com.elong.businesstravel.protocol.ProtocolDef;
import com.umeng.update.UmengUpdateAgent;

abstract public class BaseELongHttpRequest extends BaseHttpRequest {

	private JSONObject mRequstJSON;
	private String mAction;
	/** true参数加密，false参数不加密 */
	private boolean isCrypt = true;

	public BaseELongHttpRequest(Context context) {
		super(context);
	}

	@Override
	public Context getContext() {
		return mContextWeakReference.get();
	}

	@Override
	public String getAbsoluteUrl() {
		return ProtocolDef.DOMAIN;
	}

	@Override
	public Header[] getHeaders() {
		return null;
	}

	@Override
	public RequestParams getRequestParams() {
		return null;
	}

	@Override
	public String getPostContentType() {
		return null;
	}

	@Override
	public HttpEntity getRequestEntity() {

		HttpEntity httpEntity = null;
		try {
			RequestParams requestParams = createRequestParams();
			if (isCrypt) {
				httpEntity = new StringEntity(mCrypt(requestParams));
			} else {
				httpEntity = new StringEntity(requestParams.toString());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return httpEntity;
	}

	/**
	 * true参数加密，false参数不加密
	 * 
	 * @param isCrypt
	 */
	public void setCrypt(boolean isCrypt) {
		this.isCrypt = isCrypt;
	}

	/**
	 * 对参数进行加密
	 * 
	 * @param requestParams
	 * @return
	 * @throws Exception
	 */
	private String mCrypt(RequestParams requestParams) throws Exception {
		MCrypt mCrypt = new MCrypt();
		String paramString = MCrypt.bytesToHex(mCrypt.encrypt(requestParams
				.toString()));
		return paramString;
	}

	/**
	 * 创建请求参数数据格式
	 * 
	 * @return
	 * @throws Exception
	 */
	protected RequestParams createRequestParams() throws Exception {
		RequestParams requestParams = new RequestParams();
		requestParams.add("action", URLEncoder.encode(mAction, getEncoding()));
		requestParams.add("req",
				URLEncoder.encode(mRequstJSON.toString(), getEncoding()));
		return requestParams;
	}

	/**
	 * 设置请求动作，和json参数 必须在构造方法中设置
	 * 
	 * @param action
	 * @param reqJSON
	 */
	public void setRequestActionParam(String action, JSONObject reqJSON) {
		mAction = action;
		mRequstJSON = reqJSON;
		try {
			mRequstJSON.put("uid",
					AppUserId.getUserId(mContextWeakReference.get()));
			mRequstJSON.put("deviceid",
					AppUserId.getDeviceId(mContextWeakReference.get()));
			mRequstJSON.put("uuid",
					AppUserId.getUUId(mContextWeakReference.get()));
			mRequstJSON.put("version", Version.versionName);
			// 渠道标识
			mRequstJSON.put(
					"comefrom",
					AndroidManifestUtil.getMetaValue(
							mContextWeakReference.get(), "UMENG_CHANNEL"));

			DebugLog.logI("Action : " + action + "  req : "
					+ mRequstJSON.toString());

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
